(function (G) {
    G.vue({
        data: {
            models: {
                title: {
                    label: '指令下发后请在"消息中心/指令下达/我发起"查看进度',
                    type: 'title'
                },
                images: {
                    rule:{ NotNull:true },
                    attr:{
                        limit:9,
                        sourceType:['camera']
                    },
                    label: '图片',
                    type: 'image'
                },
                description: {
                    label: '',
                    attr: {
                        placeholder: '描述信息,不超过300字',
                        maxlength: 300
                    },
                    type: 'textarea'
                },
                unitId:{
                    label:'所属区域',
                    rule:{ NotNull:true },
                    attr:{
                        actions:[
                            {name:'杨家坪环卫所',value:'hw_58D84D3F9D5E460AAD0F132BC59EB30D'},
                            {name:'石桥铺环卫所',value:'hw_F8162D79BCEF4B8D930DB385082707DD'},
                            {name:'中梁山环卫所',value:'hw_FF59774B7BE84ADC9CAE606DD83C527F'},
                            {name:'设施科',value:'hw_588058E2EAF14294AC9A539B1AA96CA3'},
                            {name:'固废生产科',value:'hw_AF985C3420C344A198A320CA423234C5'},
                            {name:'智慧环卫办公室',value:'hw'}
                        ]
                    },
                    type:'select'
                }
            },
            address: '',
            latitude: 0,
            longitude: 0,
            timer: 0,
            unit:{},
            value:{}
        },
        methods: {
            save() {
                var data = this.selectComponent('#form').submit()
                if (!data) return false
                let userInfo = G.Storage.get('userInfo')
                data.address = this.data.address
                data.longitude = this.data.longitude
                data.latitude = this.data.latitude
                data.applicantName = userInfo.name
                if(/undefined/.test(data.images)) {
                    G.alert('上传的图片存在问题，请点击未显示的图片进行删除后重新上传。')
                    return false
                }
                // console.log(data)
                // return false
                G.post('/v1/task/add', {
                    userId: userInfo.userId,
                    flowId: '1598155201922076672',
                    data,
                    des: this.data.address,
                    unitId:data.unitId,
                    // expand1:this.data.address,
                    expand2: `${this.data.longitude},${this.data.latitude}`,
                    // toUsers:toUsers,
                    title: `[${userInfo.name}]指派任务-${data.description || ''}`
                }).then(res => {
                    if (!res.errorMsg) {
                        G.toask('指令下达成功')
                        setTimeout(() => {
                            this.$back()
                        }, 2000)
                        data.f_taskId = res.task.id
                        G.post('/api/model/instruction', data).then(res => {
                            if (!res.errorMsg) {
                                G.toask('指令下达成功')
                                setTimeout(() => {
                                    this.$back()
                                }, 2000)
                            }
                        })
                    }
                })
            },
            getLocation() {
                wx.getLocation({
                    type: 'gcj02',
                    success: res => {
                        let latitude = res.latitude,
                            longitude = res.longitude
                        // let latitude = 29.496417100694444,
                        //     longitude = 106.50903293185763
                        G.get('/v1/map/geocodeRegeo', {
                            longitude,
                            latitude
                        }).then(res => {
                            this.setData({
                                latitude,
                                longitude,
                                address: res.regeocode.formatted_address,
                                value:{ unitId:res.unit.unitId },
                                // value:{},
                                // unit:res.unit
                            })
                            this.selectComponent('#form').init(this.data.models,this.data.value,false)
                        })
                    }
                })
                if (!this.data.timer) {
                    this.data.timer = setInterval(() => {
                        this.getLocation()
                    }, 1000 * 10)
                }
            },
            onShow() {
                if (this.loaded) {
                    this.getLocation()
                }
            },
            onHide() {
                if (this.data.timer) {
                    clearInterval(this.data.timer)
                    this.data.timer = 0
                }
            },
            onUnload() {
                if (this.data.timer) {
                    clearInterval(this.data.timer)
                    this.data.timer = 0
                }
            }
        },
        mounted(options) {
            this.getLocation()
        }
    })
})(wx.$)